<template>
	<xn-form-container
		:title="formData.id ? '编辑' : '增加'"
		:width="700"
		:visible="visible"
		:destroy-on-close="true"
		@close="onClose"
	>
		<a-form ref="formRef" :model="formData" :rules="formRules" layout="vertical">
			<a-form-item label="规则名称：" name="rulesName">
				<a-input v-model:value="formData.rulesName" placeholder="请输入规则名称" allow-clear />
			</a-form-item>
			<a-form-item label="适用范围：" name="orgId">
				<a-tree-select
					v-model:value="formData.orgId"
					style="width: 100%"
					:dropdown-style="{ maxHeight: '400px', overflow: 'auto' }"
					placeholder="请选择部门"
					allow-clear
					tree-default-expand-all
					:tree-data="treeData"
					multiple
					:tree-default-expanded-keys="treeDefaultExpandedKeys"
					:field-names="{
						children: 'children',
						label: 'name',
						value: 'id'
					}"
				/>
			</a-form-item>
			<a-form-item label="加班最小时长/小时：" name="minOvertimeHours">
				<a-input type="number" v-model:value="formData.minOvertimeHours" placeholder="请输入加班最小时长" allow-clear />
			</a-form-item>
			<a-form-item label="平日加班减少时长/小时：" name="weekDayRule">
				<a-input type="number" v-model:value="formData.weekDayRule" placeholder="请输入平日加班减少时长" allow-clear />
			</a-form-item>
			<a-form-item label="周末加班减少时长/小时：" name="weekEnd">
				<a-input type="number" v-model:value="formData.weekEnd" placeholder="请输入周末加班减少时长" allow-clear />
			</a-form-item>
		</a-form>
		<template #footer>
			<a-button style="margin-right: 8px" @click="onClose">关闭</a-button>
			<a-button type="primary" @click="onSubmit" :loading="submitLoading">保存</a-button>
		</template>
	</xn-form-container>
</template>

<script setup name="sysOvertimeCalcRulesForm">
	import { cloneDeep } from 'lodash-es'
	import { required } from '@/utils/formRules'
	import sysOvertimeCalcRulesApi from '@/api/biz/sysOvertimeCalcRulesApi'
	import userApi from '@/api/sys/userApi'
	// 抽屉状态
	const visible = ref(false)
	const emit = defineEmits({ successful: null })
	const formRef = ref()
	// 表单数据
	const formData = ref({})
	const submitLoading = ref(false)
	const treeDefaultExpandedKeys = ref([])
	const treeData = ref([])

	// 打开抽屉
	const onOpen = (record) => {
		visible.value = true
		if (record) {
			let recordData = cloneDeep(record)
			formData.value = Object.assign({}, recordData)
			formData.value.orgId = formData.value.orgId.split(',')
		} else {
			formData.value.minOvertimeHours = 0
			formData.value.weekDayRule = 0
			formData.value.weekEnd = 0
		}
		nextTick(() => {
			// 机构选择器数据
			userApi.userOrgTreeSelector().then((res) => {
				if (res !== null) {
					treeData.value = res
					// 默认展开2级
					treeData.value.forEach((item) => {
						// 因为0的顶级
						if (item.parentId === '0') {
							treeDefaultExpandedKeys.value.push(item.id)
							// 取到下级ID
							if (item.children) {
								item.children.forEach((items) => {
									treeDefaultExpandedKeys.value.push(items.id)
								})
							}
						}
					})
				}
			})
		})
	}
	// 关闭抽屉
	const onClose = () => {
		formRef.value.resetFields()
		formData.value = {}
		visible.value = false
	}
	// 默认要校验的
	const formRules = {
		rulesName: [required('请输入规则名称')],
		orgId: [required('请输入适用范围')],
		minOvertimeHours: [required('请输入加班最小时长')],
	}
	// 验证并提交数据
	const onSubmit = () => {
		formRef.value.validate().then(() => {
			submitLoading.value = true
			const formDataParam = cloneDeep(formData.value)
			formDataParam.orgId = formDataParam.orgId.join(',')
			sysOvertimeCalcRulesApi
				.sysOvertimeCalcRulesSubmitForm(formDataParam, formDataParam.id)
				.then(() => {
					onClose()
					emit('successful')
				})
				.finally(() => {
					submitLoading.value = false
				})
		})
	}
	// 抛出函数
	defineExpose({
		onOpen
	})
</script>
